package com.helpyoutimely.pay;/**
 * ${PROJET_NAME}
 * Created by lixinkun on 16/4/14 09:05.
 * Email EricLi1235@gmail.com.
 */

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RatingBar;
import android.widget.TextView;

import com.helpyoutimely.R;
import com.helpyoutimely.order.OrderDetailActivity;
import com.helpyoutimely.widget.LoadingDialog;

/**
 * User: lixinkun
 * Date: 2016-04-14
 * Time: 09:05
 * FIXME
 */
public class PayActivity extends Activity implements View.OnClickListener{

    private int free;
    private int discountfree;
    private int totalFree;
    private int tips;
    private int rate;

    private TelephonyManager manager ;
    private String result;
    private boolean canInput;

    private RatingBar ratingBar;
    private RadioButton rbGiveTip;
    private EditText etTips;
    private EditText etComment;
    private Button btnSubmit;
    private ImageButton btnBack;
    private TextView tvCount;
    private TextView tvFree,tvDiscountFree;
    private LoadingDialog loadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_pay);
        initData();
        initView();
    }

    private void initData(){

        free = 3;
        discountfree = 2;
        canInput = false;
    }

    private void initView(){
        rbGiveTip = (RadioButton) findViewById(R.id.rb_tip);
        etComment = (EditText) findViewById(R.id.et_comment);
        etTips = (EditText) findViewById(R.id.et_money);
        btnSubmit = (Button) findViewById(R.id.btn_pay);
        btnBack = (ImageButton) findViewById(R.id.btn_back);
        tvCount = (TextView) findViewById(R.id.tv_total_free);
        tvFree = (TextView) findViewById(R.id.tv_free);
        ratingBar = (RatingBar) findViewById(R.id.ratingBar);
        tvDiscountFree = (TextView) findViewById(R.id.tv_pay_num);
        rate = ratingBar.getNumStars();
        tvFree.setText(free+"");
        tvCount.setText(""+discountfree);
        tvDiscountFree.setText(discountfree+"");

        etTips.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String s = charSequence.toString();
                int tip;
                if(s != null && s.length()>0 && s != "") {
                    tip = Integer.parseInt(s);
                }else {
                    tip = 0;
                }
                totalFree = tip + discountfree;
                tvCount.setText(totalFree+"");


            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        etTips.setEnabled(canInput);
        btnBack.setOnClickListener(this);
        rbGiveTip.setOnClickListener(this);
        btnSubmit.setOnClickListener(this);
    }


    private void setCanInput(){
        canInput = !canInput;
        rbGiveTip.setChecked(canInput);
        etTips.setEnabled(canInput);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_back:
                finish();
                break;
            case R.id.rb_tip:
                setCanInput();
                break;
            case R.id.btn_pay:
//                if (loadingDialog == null) {
//                    loadingDialog = LoadingDialog.createDialog(this);
//                    loadingDialog.setMessage("正在提交…………");
//                    loadingDialog.setCanceledOnTouchOutside(false);
//                }
//                loadingDialog.show();
                Intent i = new Intent(this,OrderDetailActivity.class);
                startActivity(i);
                finish();
                break;
            default:
                break;
        }
    }
}
